<?php
	include_once(dirname(__DIR__) . "/include/dbconnection.php");
	include_once(dirname(__DIR__) . "/include/util.php");

	class Thread {
		private $thread_id, $forum_id, $owner_userprofile_id, $name, $closed, $last_activity, $creation;
		private $posts = array();
		
		private $html;
		
		function Thread($thread_id) {
			$this -> loadThreadFromDatabase($thread_id);
			$this -> loadPostsFromDatabase($thread_id);
			$this -> generateThreadHTML();
			$this -> generateNewPostForm();
		}
		
		private function loadThreadFromDatabase($thread_id) {
			$query = "SELECT thread_id, forum_id, owner_userprofile_id, name, closed, last_activity, creation FROM t_thread WHERE thread_id = " . intval($thread_id);
			$result = mysql_query($query);
			$row = mysql_fetch_assoc($result);

			$this -> thread_id = $row['thread_id'];
			$this -> forum_id = $row['forum_id'];
			$this -> owner_userprofile_id = $row['owner_userprofile_id'];
			$this -> name = $row['name'];
			$this -> closed = $row['closed'];
			$this -> last_activity = $row['last_activity'];
			$this -> creation = $row['creation'];

			mysql_free_result($result);
		}
		
		private function loadPostsFromDatabase($thread_id) {
			$query = "SELECT post_id, owner_userprofile_id, creation, sequence_number, content FROM t_post WHERE thread_id = " . intval($thread_id);
			$result = mysql_query($query);
			$row_count = mysql_num_rows($result);
			
			for($row_counter = 0; $row_counter < $row_count; $row_counter++) {
				$row = mysql_fetch_assoc($result);
				$this -> posts[] = $row;
			}
			
			mysql_free_result($result);
		}
		
		//Generate HTML
		
		private function generateThreadHTML() {
			$this -> html =
				"<div id=\"thread\">".
					"<h2>Thread: ". $this -> name ."</h2>";
			
			foreach($this -> posts as $post) {
				$this -> html .=
					"<div class=\"post\">".
						"Post von: ". $post['owner_userprofile_id'] ."<br />".
						$post['content'] ."<br />".
						"Erstellt am: ". $post['creation'] ."<br />".
						"<a href=\"". getUrl() ."&delete=". $post['post_id'] ."\">l&ouml;schen</a>".
					"</div><br />";
			}
			
			$this -> html .=
				"</div>";
		}
		
		private function generateNewPostForm() {
			$this -> html .=
				"<div id=\"threadform\">".
					"<h2>Neuen Post erstellen:</h2>".
					"<form action=\"". getUrl() ."&new"."\" method=\"post\" enctype=\"multipart/form-data\">".
						"<p>Content:   <textarea name=\"content\" cols=\"52\" rows=\"10\"></textarea></p>".
						"<p><input type=\"submit\" value=\"submit\"> <input type=\"reset\" value=\"reset\"></p>".
					"</form>".
				"</div>";
		}
		
		public function show() {
			echo $this -> html;
		}
	}
	
	function insertThread($forum_id, $owner_userprofile_id, $name) {
		$query = "INSERT INTO t_thread (forum_id, owner_userprofile_id, name, closed, last_activity, creation) VALUES (". intval($forum_id) .", ". intval($owner_userprofile_id) .", \"". mysql_escape_string($name) ."\", 0, NOW(), NOW())";
		mysql_query($query);
	}
	
	function closeThread($thread_id) {
		$query = "UPDATE t_thread SET closed = 1 WHERE thread_id = ". intval($thread_id);
		mysql_query($query);
	}
?>